Python Program to Swap Two Numbers using Without Temporary Variable in 4 different Methods

 

Python Program to Swap Two Numbers Without Temporary Variable in 4 different Methods

Welcome to DailyCodeHub.

If you are learning Python or preparing for coding interviews. This is one of the most common questions you will come across - swapping two numbers without using a temporary variable. It may look like a very basic problem but it actually plays an important role in building your programming logic.

At first most beginners think this problem is too easy because they already know how to swap values using a third variable. but when the condition is changed to " without using a temporary variable" many people get confused. that is exactly why interviewers like to ask this question. 

This type of problem helps them understand how you think, how you handle variables and whether you can apply logic in a simple but effective way.

I personally suggest that instead of just reading the code, you should try each method on your own. When you execute the code step by step, you will understand the concept much more clearly.

In this article we will go through 4 different methods to swap two numbers without using a temporary variable. each method uses a different idea, so by the end of this post, you will have a strong understanding of how swapping works iinternally.

Before we start-let us understand the basics :

Swapping means exchanging the values of two variables.

for example :

a = 5 and b = 10

after swapping :

a = 10 and b = 5

Normally we use third variable like this :

temp = a

a = b

b = temp

But in interviews you are often asked to solve it without using this extra variable. So let us now look at different ways to solve that.

Question :

Write a Python program to swap two integers without using a temporary variable.

Now let us solve this problem in different ways.

Method 1 : Using Arithmetic operators 

Code :

# Author: P.Prabhas
# Take input from user a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) # Print values before swapping print("Before swapping:", a, b) # Swap without using third variable a = a + b b = a - b a = a - b # Print values after swapping print("After swapping:", a, b)

Output :

Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5

How it works :

Let us take an example: a = 5 and b = 10

step 1 :

a = a + b → a = 5 + 10 = 15

Now a store both values together

step 2 :

b = a - b → b = 15 - 10 = 5

Now b gets the original value of a

step 3 :

a = a - b → a = 15 - 5 = 10

Now a gets the original value of b

Final result : a = 10, b = 5 (values swapped )

In this method we first combine both values and then separate them step by step using subtraction, so no extra variable is needed.


Method 2: Using the XOR operator

Code :

# Author: P.Prabhas
# Take input from user
a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) # Print values before swapping print("Before swapping:", a, b) # Swap using XOR operator a = a ^ b b = a ^ b a = a ^ b # Print values after swapping print("After swapping:", a, b)

Output :

Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5

How it works :

Let's take an example: a = 5 and b = 10

Let us see how values change step by step :

Step1 :

a = a ^ b

Step2 :

b = a ^ b

Step3:

a = a ^ b

Final result: a = 10, b = 5 (values swapped)

In this method XOR combines and restores values. applying XOR twice helps us recover the original values without using extra space.

Method 3 : Using Python Tuple 

Code :


# Author: P.Prabhas

# Take input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

# Print values before swapping
print("Before swapping:", a, b)

# Swap using tuple unpacking
a, b = b, a

# Print values after swapping
print("After swapping:", a, b)

Output :

Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5


How it works :

Let's take an example: a = 5 and b = 10

Here is a single step :
a, b = b, a
Python first takes values (b, a) → (10, 5)
Then it assigns them → a = 10, b = 5

Final result: a = 10, b = 5 (values swapped)
In this method Python automatically swaps values in one line, making it the simplest and cleanest approach.

Method 4 : Using multiplication & division

Code :

# Author: P.Prabhas

# Take input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

# Print values before swapping
print("Before swapping:", a, b)

# Swap using multiplication and division
a = a * b
b = a // b
a = a // b

# Print values after swapping
print("After swapping:", a, b)

Output :

Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5

Important note :

  • Do not use when one value is 0
  • It causes overflow for large values

How it works :
Let's take an example: a = 5 and b = 10
Let us see how values change step by step

Step 1 :
a = a * b → a = 5 × 10 = 50
Step 2 :
b = a // b → b = 50 // 10 = 5
Step 3 :
a = a // b → a = 50 // 5 = 10

Final result : a = 10, b = 5 (values swapped)
In this method we combine values using multiplication and then recover them using division.

Interview tip :
 In interview, start by explaining the arithmetic method clearly. after that you can mention
the tuple method as a cleaner Python approach.
This shows both your logical thinking and your knowledge of Python features.

You can also read this:
Frequently asked Questions (FAQ's) :

1. what is swapping in Python?
Swapping means exchanging the values of two variables. for example if a = 5 and b = 10 after swapping a becomes 10 and b becomes 5.

2. Can we swap two numbers without using a third variable?
Yes , we can swap two numbers without using a temporary variable by using methods like arithmetic operations, XOR or tuple assignment.

3. Which method is best for swapping in python?
The tuple method is the best because it is simple, readable and widely used in python programs

4. Why is swapping an important concept in programming?
Swapping helps you understand how variables store and exchange values. it is also used in many algorithms and logical problems.

5. Is the multiplication and division method safe for swapping?
No, it is not always safe because it does not work when one of the values is zero and may cause errors with large numbers.

Conclusion :

Swapping two integers without using a temporary variable looks like a small problem, but it helps you understand how variables and operations work internally.

In this post we have explored 4 different methods. Each method follows a different approach, so try to understand the logic behind them instead of just memorizing the code.

Among all methods tuple swapping is the most recommended in Python because it is simple, readable and efficient.

Practice these methods with different values and try to implement them on your own. this will improve your confidence and help you to perform better in interviews.

That's all for today, friends.
Keep coding, keep learning.
DailyCodeHub

No comments:

Post a Comment